const app = getApp();
Page({
  data: {
    // tabs: ['推荐', '历史', '故事', '美图', '视频', '搞笑'],
    tabs: ['推荐', '历史', '故事'],
    activeTab: 0,
    listData: [[], [], [], [], [], []], // 对应5个分类数据
    pageSizes: [10, 10, 10, 10, 10, 10], // 每页条数
    currentPages: [1, 1, 1, 1, 1],   // 当前页码
    hasMore: [true, true, true, true, true, true], // 是否还有更多
    loading: false
  },

  onLoad() {
    this.loadData();
  },

  // 切换分类
  switchTab(e) {
    const index = e.currentTarget.dataset.index;
    console.log(index);
    this.setData({ activeTab: index }, () => {
      if (this.data.listData[index].length === 0) {
        this.loadData();
      }
    });
  },

  // 加载数据
  async loadData() {
    if (this.data.loading || !this.data.hasMore[this.data.activeTab]) return;
    this.setData({ loading: true });
    // 模拟接口请求
    const newData = await this.mockApi();
    console.log("newData");
    console.log(newData);
    const { activeTab, currentPages, listData } = this.data;
    
    this.setData({
      [`listData[${activeTab}]`]: [...listData[activeTab], ...newData],
      [`currentPages[${activeTab}]`]: currentPages[activeTab] + 1,
      loading: false,
      [`hasMore[${activeTab}]`]: newData.length >= this.data.pageSizes[activeTab]
    });
  },

  // 加载更多
  loadMore() {
    this.loadData();
  },

  // 模拟数据接口
  mockApi() {
    var activeVar = this.data.activeTab;
    // app.wxRequest('POST', app.globalData.URL + '/news/getNewsList', {"pageNum":this.data.currentPages[this.data.activeTab],"pageSize":10,"typeCode":activeVar}, (res) => {
    //   this.setData({
    //     cardInfo: res.data.rows
    //   });
    // }, (err) => {
    //   console.log(err.errMsg)
    // });

    return new Promise(resolve => {
      app.wxRequest('POST', app.globalData.URL + '/news/getNewsList', {"pageNum":this.data.currentPages[this.data.activeTab],"pageSize":10,"typeCode":activeVar}, (res) => {
        resolve(res.data.rows);
      }, (err) => {
        console.log(err.errMsg)
      });
    });

    // return new Promise(resolve => {
    //   setTimeout(() => {
    //     const data = Array(10).fill().map((_,i) => ({
    //       id: Date.now() + i,
    //       img: `https://gateway.juxue.cloud/resmanage/resourcesManage/download?bucketName=1678655958080851969-img&fileName=1678656149325549570/50ab557d-edaa-4ad4-962a-eb07ceac5a7e.jpg`,
    //       title: `内容标题 ${this.data.currentPages[this.data.activeTab]}-${i+1}`,
    //       desc: '精选内容描述，展示当前分类下的特色信息'
    //     }));
    //     resolve(data);
    //   }, 800);
    // });
    
  },
  onCardTap: function(event) {
    const itemId = event.currentTarget.dataset.itemId;  // 获取传递的数据
    // 根据 itemId 进行相应的操作，例如跳转到详情页
    wx.navigateTo({
      // url: `/pages/detail/detail?id=${itemId}`  // 假设详情页路径为 /pages/detail/detail
      url: `/pages/d2/d2?id=${itemId}`  // 假设详情页路径为 /pages/detail/detail
    });
  },
  onShareAppMessage() {
    return {
      title: '文字与光影的诗意栖居',
      path: '/pages/index/index'
    }
  },
  
})